package com.sansan.qiangji.controller.system;

import com.sansan.qiangji.enums.ResultEnum;
import com.sansan.qiangji.exception.ServiceException;
import com.sansan.qiangji.form.system.user.UserEditForm;
import com.sansan.qiangji.form.system.user.UserListForm;
import com.sansan.qiangji.form.system.user.UserMenuPowerEditForm;
import com.sansan.qiangji.form.system.user.UserRegisterForm;
import com.sansan.qiangji.realize.system.UserReal;
import com.sansan.qiangji.resultVO.ResultVO;
import com.sansan.qiangji.utils.ResultVOUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

@CrossOrigin()
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {
    private UserReal userReal;

    @Resource
    public void setUserReal(UserReal userReal) {
        this.userReal = userReal;
    }

    @PostMapping("/register")
    public ResultVO<Object> userRegister(@Valid @RequestBody UserRegisterForm userRegisterForm, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            log.error("【创建用户】参数不正确，userRegisterForm={}", userRegisterForm.toString());
            throw new ServiceException(ResultEnum.PARAM_ERROR.getCode(), Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage());
        }
        userReal.userRegister(userRegisterForm);
        return ResultVOUtil.success(null);
    }

    @PostMapping("/list")
    public ResultVO<Object> getUserList(@Valid @RequestBody UserListForm userListForm, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            log.error("【用户列表】参数不正确，userListForm={}", userListForm.toString());
            throw new ServiceException(ResultEnum.PARAM_ERROR.getCode(), Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage());
        }
        return ResultVOUtil.success(userReal.userList(userListForm));
    }

    @PostMapping("/editUser")
    public ResultVO<Object> editUser(@Valid @RequestBody UserEditForm userEditForm, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            log.error("【用户列表】参数不正确，userEditForm={}", userEditForm.toString());
            throw new ServiceException(ResultEnum.PARAM_ERROR.getCode(), Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage());
        }
        userReal.editUser(userEditForm);
        return ResultVOUtil.success(null);
    }

    @PostMapping("/login")
    public ResultVO<Object> userLogin(@RequestParam(name = "userName") String userName, @RequestParam(name = "userPassword") String userPassword) {
        return ResultVOUtil.success(userReal.userLogin(userName, userPassword));
    }

    @PostMapping("/delete")
    public ResultVO<Object> deleteUser(@RequestParam(name = "userId") Integer userId) {
        userReal.deleteUser(userId);
        return ResultVOUtil.success(null);
    }

    @PostMapping("/changeStatus")
    public ResultVO<Object> changeUserStatus(@RequestParam(name = "userId") Integer userId) {
        userReal.changeUserStatus(userId);
        return ResultVOUtil.success(null);
    }

    @PostMapping("/editUserRole")
    public ResultVO<Object> editUserRole(@RequestParam(name = "userId") Integer userId, @RequestParam(name = "roleName") String roleName) {
        userReal.editUserRole(userId, roleName);
        return ResultVOUtil.success(null);
    }

    @PostMapping("/userMenuPowerEdit")
    public ResultVO<Object> userMenuPowerEdit(@Valid @RequestBody UserMenuPowerEditForm userMenuPowerEditForm, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            log.error("【角色菜单权限编辑】参数不正确，userMenuPowerEditForm={}", userMenuPowerEditForm.toString());
            throw new ServiceException(ResultEnum.PARAM_ERROR.getCode(), Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage());
        }
        userReal.userMenuPowerEdit(userMenuPowerEditForm);
        return ResultVOUtil.success(null);
    }

    @PostMapping("/userPowerEdit")
    public ResultVO<Object> userPowerEdit(@RequestParam(name = "userId") Integer userId, @RequestParam(name = "powerName") String powerName) {
        userReal.userPowerEdit(userId, powerName);
        return ResultVOUtil.success(null);
    }
}
